package com.lishu.carsafe.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lishu.carsafe.R;
import com.lishu.carsafe.http.HttpBase;
import com.lishu.carsafe.http.HttpLoader;
import com.lishu.carsafe.model.BaseModel;
import com.lishu.carsafe.model.TaskDetailModel;
import com.lishu.carsafe.utils.BitmapUtil;
import com.lishu.carsafe.utils.ImageUtil;
import com.lishu.carsafe.utils.NetUtil;
import com.lishu.carsafe.utils.ToastUtil;

public class TaskUnprocessedDetailActivity extends BaseActivity implements View.OnClickListener{
    private ImageView task_picture, video_icon;
    private TextView task_state;
    private TextView task_title;
    private TextView location;
    private TextView task_detail;
    private TextView start_time;
    private TextView end_time;
    private TextView duration_time;
    private String taskId;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 2:
                    String path2 = (String) msg.obj;
                    Bitmap bitmap2 = BitmapUtil.getNetVideoBitmap(path2);
                    task_picture.setImageBitmap(bitmap2);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unprocessed_task_detail);

        taskId = getIntent().getStringExtra("taskId");

        initView();
        getTaskDetail(taskId);
    }

    private void initView() {
        setTopTitle("任务详情");
        task_picture = findViewById(R.id.task_picture);
        video_icon = findViewById(R.id.video_icon);
        task_state = findViewById(R.id.task_state);
        task_title = findViewById(R.id.task_title);
        location = findViewById(R.id.location);
        task_detail = findViewById(R.id.task_detail);
        start_time = findViewById(R.id.start_time);
        end_time = findViewById(R.id.end_time);
        duration_time = findViewById(R.id.duration_time);
        findViewById(R.id.process_button).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.process_button:
                Intent intent = new Intent(TaskUnprocessedDetailActivity.this, TaskDisposeActivity.class);
                intent.putExtra("taskId", taskId);
                startActivity(intent);
                break;
        }
    }

    /**
     * 获取详情
     */
    private void getTaskDetail(String taskId) {
        HttpLoader.getTaskDetail(taskId, new HttpBase.IResponseListener() {
            @Override
            public void onResponse(BaseModel model) {
                if (model == null) {
                    ToastUtil.showShort(R.string.please_check_network);
                    return;
                }
                if (!model.success()) {
                    ToastUtil.showShort(getString(R.string.get_data_fail) + model.getMsg());
                    return;
                }

                TaskDetailModel.TaskDetailBean taskDetail = ((TaskDetailModel) model).getData();
                showTaskDetailUI(taskDetail);
            }
        });
    }

    private void showTaskDetailUI(TaskDetailModel.TaskDetailBean taskDetail) {
        if(taskDetail != null){
            String status = taskDetail.getState();//0未完成1已完成2超时未完成 3超时完成
            if("1".equals(status)){
                task_state.setText("已完成");
            }else if("2".equals(status)){
                task_state.setText("超时未完成");
            }else if("3".equals(status)){
                task_state.setText("超时完成");
            }else{
                task_state.setText("未完成");
            }

            task_title.setText(taskDetail.getTitle());
            location.setText(taskDetail.getAddress());
            task_detail.setText(taskDetail.getContent());
            start_time.setText(taskDetail.getStartTime());
            end_time.setText(taskDetail.getEndTime());
            duration_time.setText(taskDetail.getPredictDuration());

            String type = taskDetail.getMediaType();//任务多媒体类型0照片1视频
            String path = taskDetail.getMediaUrl();
            if(!TextUtils.isEmpty(path)) {
                final String fullPath = NetUtil.getFullPath(path);
                if("0".equals(type)) {
                    ImageUtil.bindImageView(task_picture, NetUtil.getFullPath(path), R.drawable.ic_default_photo);
                    video_icon.setVisibility(View.INVISIBLE);
                    task_picture.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(mContext, PhotoViewActivity.class);
                            intent.putExtra("imagePath", fullPath);
                            startActivity(intent);
                        }
                    });
                }else {
                    video_icon.setVisibility(View.VISIBLE);
                    showVideoFirstFrame(2, fullPath);
                    task_picture.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(mContext, PlayVideoActivity.class);
                            intent.putExtra("videoPath", fullPath);
                            startActivity(intent);
                        }
                    });
                }
            }
        }
    }

    private void showVideoFirstFrame(int what, String path){
        Message msg = Message.obtain();
        msg.what = what;
        msg.obj = path;
        mHandler.sendMessage(msg);
    }
}
